﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Text;

using PdfCreative.Pdf.Graphics;

namespace RenderingText
{
    class Program
    {
        static void Main(string[] args)
        {
            // We will create 'HelloWorld.pdf' in the user's temporary folder.
            string path = Path.Combine(Path.GetTempPath(), "RenderingText.pdf");

            // Create a new Graphics instance.
            Graphics g = new Graphics(path);

            // Render the text.
            RenderText(g);

            // Close the graphics.
            g.Close();

            // Open the PDF document.
            Process.Start(path);
        }

        /// <summary>
        /// Render text in various formats using PDF Graphics .NET
        /// </summary>
        /// <param name="g">The Graphics instance.</param>
        static void RenderText(Graphics g)
        {
            // Create a font.
            Font font = new Font("Times New Roman");
            Font italic = new Font("Times New Roman Italic");

            RenderSample1(g, font);

            RenderSample2(g, font);

            RenderSample3(g, font);

            RenderSample4(g, font);

            RenderSample5(g, font,italic);

        }

        static void RenderSample1(Graphics g, Font font)
        {
            // 1. Text on a single line
            g.DrawString(Rgb.BlackBrush, "1. Text on a single line, without word breaks.", font, 10f, 36f, 36f);

            g.TransformTranslate(0, 36f);
        }

        static void RenderSample2(Graphics g, Font font)
        {
            // 2. Text in a bounding box
            Rectangle bounds = new Rectangle(36f, 72f, 144f, 36f);
            g.DrawString(Rgb.BlackBrush, "2. Text in a bounding box, with word breaks where neccessary.", font, 10f, bounds);
            g.DrawRectangle(Rgb.RedPen, bounds);

            g.TransformTranslate(0, bounds.Height+36f);
        }

        static void RenderSample3(Graphics g, Font font)
        {
            // 3. Various string formatting options.
            StringFormat format = new StringFormat();
            Rectangle bounds = new Rectangle(36f, 72f, 144f, 36f);

            foreach(HorizontalAlignment align in new []{ HorizontalAlignment.Left, HorizontalAlignment.Center, HorizontalAlignment.Right })
            {
                format.HorizontalAlignment = align;
                g.DrawString(
                    Rgb.BlackBrush, 
                    "3. Text drawn with various formatting options - horizontal alignment", 
                    font, 
                    10f, 
                    bounds,
                    format);

                g.DrawRectangle(Rgb.RedPen, bounds);
                
                bounds.Move(bounds.Width + 36f, 0);
            }

            g.TransformTranslate(0, bounds.Height + 36f);
        }

        static void RenderSample4(Graphics g, Font font)
        {
            // 4. Various string formatting options.
            StringFormat format = new StringFormat();
            Rectangle bounds = new Rectangle(36f, 72f, 144f, 36f);

            format.LineSpacing = 10f;
            g.DrawString(
                Rgb.BlackBrush,
                "4. Text drawn with double line spacing.",
                font,
                10f,
                bounds,
                format);

            g.DrawRectangle(Rgb.RedPen, bounds);
            bounds.Move(bounds.Width + 36f, 0);

            format.LineSpacing = 0f;
            format.CharacterSpacing = 5f;
            g.DrawString(
                Rgb.BlackBrush,
                "4. Text drawn with character spacing.",
                font,
                10f,
                bounds,
                format);

            g.DrawRectangle(Rgb.RedPen, bounds);
            bounds.Move(bounds.Width + 36f, 0);

            format.CharacterSpacing = 0f;
            format.TextRatio = 1.5f;
            g.DrawString(
                Rgb.BlackBrush,
                "4. Text drawn with a wide text ratio - it appears stretched out.",
                font,
                10f,
                bounds,
                format);

            g.DrawRectangle(Rgb.RedPen, bounds);
            bounds.Move(bounds.Width + 36f, 0);

        }

        static void RenderSample5(Graphics g, Font font, Font italic)
        {
            // 5. Drawing styled text.
            Rectangle bounds = new Rectangle(36f, 72f, 144f, 40f);

            g.TransformTranslate(0, bounds.Height + 36f);

            StyledText text = new StyledText()
                .Append("5. Drawing fully styled text, with ", Rgb.BlackBrush, font, 10)
                .Append("different fonts", Rgb.BlackBrush, italic, 10)
                .Append(", and various ", Rgb.BlackBrush, font, 10)
                .Append("sizes", Rgb.BlackBrush, font, 15f)
                .Append(" and ", Rgb.BlackBrush, font, 10f)
                .Append("colours", Rgb.RedBrush, font, 10f);

            g.DrawStyledText(text, bounds, new StringFormat());

            g.DrawRectangle(Rgb.RedPen, bounds);

        }
    }
}
